package com.sdy.healthcode.h5.controller.company;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.healthcode.biz.constants.Constants;
import com.sdy.healthcode.biz.model.ResumptionApply;
import com.sdy.healthcode.biz.service.ResumptionApplyService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/resumption")
@Api(tags = "复工申请相关接口（完善中）")
public class ResumptionController extends BaseController{

    @Autowired
    private ResumptionApplyService applyService;

    public static String randomNum() {
        // 获取随机申请订单
        long dateEx = System.currentTimeMillis();
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String timeEx = dateFormat.format(dateEx);
        int aEx = (int) (Math.random() * 900 + 100);
        String randomThree = Integer.toString(aEx);
        String randomNum = timeEx + randomThree;
        return randomNum;
    }

    @PostMapping("/apply")
    @ApiOperation(value = "复工申请登记", response = ResumptionApply.class)
    public Response<ResumptionApply> apply(HttpServletRequest request, @RequestBody
    @ApiParam(name = "ResumptionApply", value = "实体类") ResumptionApply resumptionApply) throws BizException {
        Date date = new Date();
        Integer userId = getUserId(request);
        resumptionApply.setApplyUserId(userId).setCreateTime(date).setApplyNum(randomNum()).setApprovalResult(0);
        // 检测是否重复提交
        ResumptionApply one = applyService.getOne(Wrappers.<ResumptionApply>lambdaQuery()
                .eq(ResumptionApply::getUscc, resumptionApply.getUscc())
                .eq(ResumptionApply::getApplyUserId, userId));
        if (one != null) {
            return Response.error("该公司已提交申请，请勿重复提交!");
        }

        boolean save = applyService.save(resumptionApply);
        Assert.isTrue(!save, "提交复工申请失败!");
        Integer id = resumptionApply.getId();
        ResumptionApply byId = applyService.getById(id);
        return Response.success(byId);
    }

    @GetMapping("/getComApply")
    @ApiOperation(value = "我司复工申请信息", response = ResumptionApply.class)
    public Response<ResumptionApply> getComApply(HttpServletRequest request) {
        Integer userId = getUserId(request, Constants.SESSION_WEB_USER_ID);
        LambdaQueryWrapper<ResumptionApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ResumptionApply::getApplyUserId, userId);
        ResumptionApply resumptionApply = applyService.getOne(wrapper);
        return Response.success(resumptionApply);
    }
}
